//
//  SearchView.m
//  homeland
//
//  Created by rain on 14-5-22.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "SearchView.h"

@interface SearchView() <UITextFieldDelegate>

@end
@implementation SearchView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (SearchView *)initView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"SearchView" owner:nil options:nil];
    
    return [nibView objectAtIndex:0];
}

- (IBAction)searchText:(id)sender;
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSearchText:)] && [self.textField.text length]) {
        [self.delegate didSearchText:self.textField.text];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarTextDidEnd:)] && self.textField.text.length == 0) {
        [self.delegate searchBarTextDidEnd:self.textField.text];
    }
    [self.textField resignFirstResponder];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.searchText = textField.text;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSearchText:)] && [self.textField.text length]) {
        [self.delegate didSearchText:self.textField.text];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarTextDidEnd:)] && self.textField.text.length == 0) {
        [self.delegate searchBarTextDidEnd:textField.text];
    }

    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
{
    self.searchText = @"";
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarTextDidEnd:)]) {
        [self.delegate searchBarTextDidEnd:textField.text];
    }
    return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
 */

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.textField.delegate = self;
}

@end
